var comment_case = '';
var comment_handel = '';
function showCommList(type,id,element,a){
	ajax.send('mode.php?m=o&q=ajax&action=showcommlist','type='+type+'&id='+id,function(){
		var rText = ajax.request.responseText.split('\t');
		if (rText[0] == 'success') {
			if (typeof(element)=='string') {
				element = getObj(element);
			}
			var whole	= elementBind('div');
			var comm_list	= elementBind('div','comm_list_'+type+'_'+id);
			whole.appendChild(comm_list);
			element.appendChild(whole);
			if (rText[1] != '') {
				var comments =  JSONParse(rText[1]);
				if (typeof(comments)=='object') {
					for (var i in comments) {
						if (typeof(comments[i])=='object') {
							creatComment(comments[i],comm_list);
						}
					}
				}
			}
			this.container = comm_list;
			comment_handel = comm_list;
			a.innerHTML = '收起回复';
			a.onclick	= function () {
				this.innerHTML = '回复(' + comm_list.getElementsByTagName('dl').length + ')';
				this.onclick = function() {
					if (whole.style.display=='none') {
						whole.style.display	= '';
						this.innerHTML	= '收起回复';
					} else {
						whole.style.display = 'none';
						this.innerHTML	= '回复(' + comm_list.getElementsByTagName('dl').length + ')';
					}
				}
				whole.style.display	= 'none';
			}

			showCommCase();
			comment_case = 'comm_p_'+type+'_'+id+'_'+0;
			var comm_post	= elementBind('div',false,'cc list-dlC');
			
			var input_p	= elementBind('p','comm_p_'+type+'_'+id+'_'+0);
			var span = elementBind('span',false,'app-icon fl');
			input_p.appendChild(span);
			
			var comm_input	= elementBind('input','comm_input_'+type+'_'+id+'_'+0,'ip','width: 200px; margin-left: 5px;');
			comm_input.setAttribute('type','text');
			comm_input.setAttribute('name','title');
			comm_input.setAttribute('maxlength','200');
			input_p.appendChild(comm_input);
			
			var comm_button = elementBind('input',false,'bt','margin-left: 5px;');
			comm_button.setAttribute('type','button');
			comm_button.setAttribute('value','回复');
			comm_button.onclick = function () {
				sendComment(type,id,0,'comm_list_'+type+'_'+id);
			}
			input_p.appendChild(comm_button);
			comm_post.appendChild(input_p);
			var hidden_p	= elementBind('p','comm_hidden_p_'+type+'_'+id,false,"display:none;");
			var hidden_a	= elementBind('a',false,false,"cursor: pointer;");
			hidden_a.onclick = function () {
				getObj('comm_p_'+type+'_'+id+'_'+0).style.display	= '';
				getObj('comm_hidden_p_'+type+'_'+id).style.display	= 'none';
				showCommCase();
				comment_case = 'comm_p_'+type+'_'+id+'_'+0;
			}
			hidden_a.innerHTML	= '回复主题';
			hidden_p.appendChild(hidden_a);
			comm_post.appendChild(hidden_p);
			this.postid	= 'comm_p_'+type+'_'+id+'_'+0;
			whole.appendChild(comm_post);
		} else {
			ajax.guide();
		}
	});
}

function showCommCase(){
	if (comment_case != '') {
		arr	= comment_case.split('_');
		if (arr[4] != 0) {
			delElement(comment_case);
		} else {
			var base_s_p = getObj('comm_p_'+arr[2]+'_'+arr[3]+'_'+0);
			base_s_p.style.display = 'none';
			var base_h_p = getObj('comm_hidden_p_'+arr[2]+'_'+arr[3]);
			base_h_p.style.display = '';
		}
	}
}

function creatComment(obj,element){
	if (typeof(obj)!='object') {
		return false;
	}
	var id	= obj['id'];
	var uid = obj['uid'];
	var type 	= obj['type'];
	var typeid 	= obj['typeid'];
	var username= obj['username'];
	var title	= obj['title'];
	var postdate= obj['postdate'];
	var upid	= obj['upid'];
	
	var dl	= elementBind('dl','comment_'+id,'cc list-dlC');
	var dt = elementBind('dt',false,'app-icon fl');

	dl.appendChild(dt);
	var dd = elementBind('dd');
	var user	= elementBind('a',false,'pdr blue');
	user.href	= modeBase + '&q=user&u=' + uid;
	user.innerHTML = username+':';
	dd.appendChild(user);
	
	var content	= elementBind('span',false,'pdr');
	content.innerHTML = title;
	dd.appendChild(content);
	
	var time	= elementBind('span',false,'gray pdr');
	time.innerHTML = postdate;
	dd.appendChild(time);
	
	if (upid==0) {
		var action	= elementBind('span',false,'action pdr');
		var reply	= elementBind('a','repliy_'+id,'flatcmt-reply','cursor: pointer;');
		reply.onclick = function () {
			if (getObj('comment_'+id)) {
				showCommInput(type,typeid,id);
			} else {
				alert('error');
			}
		};
		reply.innerHTML = '回复';
		action.appendChild(reply);
		dd.appendChild(action);
	}
	if (winduid == uid || groupid == 3) {
		var dela= elementBind('a','del_'+id,'gray b small o-del','cursor: pointer;');
		dela.onclick = function () {
			ajax.send('mode.php?m=o&q=ajax&action=commdel','id='+id,function(){
				var rText = ajax.request.responseText.split('\t');
				if (rText[0] == 'success') {
					if (comment_case=='comm_input_'+type+'_'+typeid+'_'+upid) {
						showCommCase();
						comment_case = 'comm_input_'+type+'_'+typeid+'_'+0;
					}
					delElement('comment_'+id);
				} else {
					ajax.guide();
				}
			});
		};
		dela.innerHTML = 'x';
		dd.appendChild(dela);
	}
	dl.appendChild(dd);

	if (upid != 0 && getObj('comment_'+upid)) {
		var parent = getObj('comment_'+upid);
		var dd	= parent.getElementsByTagName("dd");
		if (typeof(dd)=='object' && dd.length>0) {
			dd[0].appendChild(dl);
		}
	} else if (typeof(element)=='object') {
		element.appendChild(dl);
	}
}

function showCommInput(type,typeid,upid) {
	if (getObj('comment_'+upid) && !getObj('comm_p_'+type+'_'+typeid+'_'+upid)) {
		var thiscomm = getObj('comment_'+upid);
		var dd	= thiscomm.getElementsByTagName("dd");
		if (typeof(dd)=='object' && dd.length>0) {
			var parent	= dd[0];
		} else {
			alert('error');
			return false;
		}
		var input_p = elementBind('div','comm_p_'+type+'_'+typeid+'_'+upid,'cc list-dlC');
		var span = elementBind('span',false,'app-icon fl');
		input_p.appendChild(span);
		var comm_input	= elementBind('input','comm_input_'+type+'_'+typeid+'_'+upid,'ip','height: 14px; width: 200px; margin-left: 5px;');
		comm_input.setAttribute('type','text');
		comm_input.setAttribute('name','title');
		comm_input.setAttribute('maxlength','200');
		input_p.appendChild(comm_input);

		var comm_button = elementBind('input',false,'bt','height: 20px; margin-left: 5px; margin-top: -5px;');
		comm_button.setAttribute('type','button');
		comm_button.setAttribute('value','回复');
		comm_button.onclick = function () {
			sendComment(type,typeid,upid);
		}
		input_p.appendChild(comm_button);
		showCommCase();
		comment_case = 'comm_p_'+type+'_'+typeid+'_'+upid;
		parent.appendChild(input_p);
	}
}

function sendComment(type,typeid,upid,container){
	if (getObj('comm_input_'+type+'_'+typeid+'_'+upid)) {
		var title = getObj('comm_input_'+type+'_'+typeid+'_'+upid).value;
		if ((title.length < 3) || (title.length >= 200)) {
			showDialog('error','评论内容必须在3～200个字节之间');
			return false;
		}
	} else {
		showDialog('error','没有评论条件');
		return false;
	}
	container = objCheck(container);
	getObj('comm_input_'+type+'_'+typeid+'_'+upid).value = '';
	ajax.send('mode.php?m=o&q=ajax&action=commreply','type='+type+'&id='+typeid+'&upid='+upid+'&title='+ajax.convert(title),function(){
		var rText = ajax.request.responseText.split('\t');
		if (rText[0] == 'success') {
			var date = new Date();
			var thispost = dateFormat(date,'yyyy-mm-dd hh:ii:ss');
			var thisobj = {
				'id'		: rText[1],
				'uid'		: winduid,
				'username'	: char_cv(windid),
				'type'		: type,
				'typeid'	: typeid,
				'title'		: char_cv(title),
				'postdate'	: thispost,
				'upid'		: upid
			};
			if (upid != 0) {
				delElement('comm_p_'+type+'_'+typeid+'_'+upid);
				comment_case = '';
			}
			creatComment(thisobj,container);
		} else {
			ajax.guide();
		}
	});
}